package com.smart.campus.web.controller.school;

import java.util.List;

import com.smart.campus.school.domain.WrongQuestion;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.ExamResults;
import com.smart.campus.school.service.IExamResultsService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * resultsController
 * 
 * @author Zhihe.Li
 * @date 2020-06-29
 */
@Controller
@RequestMapping("/school/results")
public class ExamResultsController extends BaseController
{
    private String prefix = "school/results";

    @Autowired
    private IExamResultsService examResultsService;

    @RequiresPermissions("school:results:view")
    @GetMapping()
    public String results()
    {
        return prefix + "/results";
    }

    /**
     * 查询results列表
     */
    @RequiresPermissions("school:results:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamResults examResults)
    {
        startPage();
        List<ExamResults> list = examResultsService.selectExamResultsList(examResults);
        return getDataTable(list);
    }

    /**
     * 导出results列表
     */
    @RequiresPermissions("school:results:export")
    @Log(title = "results", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamResults examResults)
    {
        List<ExamResults> list = examResultsService.selectExamResultsList(examResults);
        ExcelUtil<ExamResults> util = new ExcelUtil<ExamResults>(ExamResults.class);
        return util.exportExcel(list, "results");
    }

    /**
     * 新增results
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存results
     */
    @RequiresPermissions("school:results:add")
    @Log(title = "results", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamResults examResults)
    {
        return toAjax(examResultsService.insertExamResults(examResults));
    }

    /**
     * 修改results
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamResults examResults = examResultsService.selectExamResultsById(id);
        mmap.put("examResults", examResults);
        return prefix + "/edit";
    }

    /**
     * 修改保存results
     */
    @RequiresPermissions("school:results:edit")
    @Log(title = "results", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamResults examResults)
    {
        return toAjax(examResultsService.updateExamResults(examResults));
    }

    /**
     * 删除results
     */
    @RequiresPermissions("school:results:remove")
    @Log(title = "results", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examResultsService.deleteExamResultsByIds(ids));
    }


    @RequiresPermissions("school:results:export")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<ExamResults> util = new ExcelUtil<ExamResults>(ExamResults.class);
        return util.importTemplateExcel("考试成绩");
    }

    @Log(title = "考试成绩", businessType = BusinessType.IMPORT)
    @RequiresPermissions("school:results:export")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ExamResults> util = new ExcelUtil<ExamResults>(ExamResults.class);
        List<ExamResults> list = util.importExcel(file.getInputStream());

        for (ExamResults examResults : list) {
            examResultsService.insertExamResults(examResults);
        }

        return AjaxResult.success("导入成功");
    }
}
